﻿using BacklogParser.Models.EventsData;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml.Linq;

namespace BacklogParser.Parsers
{
    
    public static class EventsDataParser
    {
        public static List<DossierEvent> Parse(List<XElement> events, string doc_id)
        {
            var result = new List<DossierEvent>();
            foreach (var dataItem in events)
            {
                var dossierEvents = dataItem.Descendants(Constants.Epo + "dossier-event").ToList();
               

                foreach (var item in dossierEvents)
                {
                    var parsed = new DossierEvent();
                    parsed.doc_id = doc_id;
                    parsed.d_index = dossierEvents.IndexOf(item).ToString();
                    parsed.event_date = item.Descendants(Constants.Epo + "event-date").FirstOrDefault().Value;
                    parsed.event_code = item.Descendants(Constants.Epo + "event-code").FirstOrDefault().Value;


                    var gazetterRef = item.Descendants(Constants.Epo + "gazette-reference").FirstOrDefault();
                    string gazetteNum = "";
                    string gazetteRefDate = "";
                    if (gazetterRef != null)
                    {
                        var gazeteNumElem = gazetterRef.Descendants(Constants.Epo + "gazette-num").FirstOrDefault();
                        var gazeteNumDate = gazetterRef.Descendants(Constants.Epo + "date").FirstOrDefault();
                        if(gazeteNumElem != null) gazetteNum = gazeteNumElem.Value;
                        if (gazeteNumDate != null) gazetteRefDate = gazeteNumDate.Value;
                    }

                    parsed.gazette_num = gazetteNum;
                    parsed.gazette_date = gazetteRefDate;



                    var dossierEventType = item.Attribute("event-type");
                    
                    if (dossierEventType != null)
                        parsed.event_type = dossierEventType.Value;


                    var dossierTexts = item.Descendants(Constants.Epo + "event-text").ToList();
                    foreach(var dossierText in dossierTexts)
                    {
                        var attr = "";
                        var textTypeAttr = dossierText.Attribute("event-text-type");
                        var textTypeAttr2 = dossierText.Attribute(Constants.Epo + "event-text-type");

                        if (textTypeAttr != null)
                            attr = textTypeAttr.Value;

                        if (textTypeAttr2 != null)
                            attr = textTypeAttr2.Value;

                        var text = dossierText.Value;

                        

                        parsed.DossierText.Add(new Models.DossierText
                        {
                            doc_id = doc_id,
                            d_index = parsed.d_index,
                            text = text,
                            text_type = attr                            
                        });

                    }
                    result.Add(parsed);
                }

            }

            return result;
        }
    }
}
